{% extends 'base.html' %}
{% block title %}
    学生列表
{% endblock %}

{% block content %}
    <p class="mt-2">{{classname}}共有{{total_count}}名学生！
    <span class="ms-3">今天是{{ strdate|date:"Y年n月j日" }} {{ weekday }}</span>
        {% for birthday in get_birthday_by_classid %}
        <span id="birthday" class="text-danger">今天是{{birthday.name}}的生日！<i class="bi bi-cake"></i></span>
        {% endfor %}
    </p>
    
    <nav aria-label="Page navigation example">
      <ul class="pagination justify-content-center">
        <li class="page-item {% if request.GET.prev|default:'0' == '-4' %}active{% endif %}"><a class="page-link" href="?prev=-4">前四天</a></li>
        <li class="page-item {% if request.GET.prev|default:'0' == '-3' %}active{% endif %}"><a class="page-link" href="?prev=-3">前三天</a></li>
        <li class="page-item {% if request.GET.prev|default:'0' == '-2' %}active{% endif %}"><a class="page-link" href="?prev=-2">前二天</a></li>
        <li class="page-item {% if request.GET.prev|default:'0' == '-1' %}active{% endif %}"><a class="page-link" href="?prev=-1">前一天</a></li>
        <li class="page-item {% if request.GET.prev|default:'0' == '0' %}active{% endif %}"><a class="page-link" href="?prev=0">今日</a></li>
        <li class="page-item"><a class="page-link" href="{% url 'core:week_manage' classid strdate %}">近一周</a></li> 
        <li class="page-item"><a class="page-link" href="#">近一月</a></li>
        <li class="page-item"><a class="page-link" href="#">本学期</a></li>
      </ul>
    </nav>
     <div class="card mt-3">
        <div class="card-header">
            代办列表
            <span id="task_number" class="badge bg-danger">您有1个任务未处理</span>
        </div>
        <div class="card-body">
            <h6 class="card-title">
                <div class="container mt-4">
                    <div id="task_list">
                       
                    </div>
                </div>
            </h6>
        </div>
    </div>

    <h6 class="mt-2">总体情况</h6>
  
     <table class="table table-sm">
        <thead>
            <tr>
                <th>迟到记录</th>
                <th>上午</th>
                <th>下午</th>
                <th>晚上</th>
                <th>请假</th>
                <th>违纪</th>
                <th>预警</th>
                <th>详细</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><i class="bi bi-check-lg text-success"></td>
                <td>{{attendance_list.late_stats.morning}}次</td>
                <td>{{attendance_list.late_stats.afternoon}}次</td>
                <td>{{attendance_list.late_stats.evening}}次</td>
                <td>{{leave_discipline.leave_count}}次</td>
                <td>{{leave_discipline.discipline_count}}次</td>
                <td></td>
                <th>
                     <a href="{% url 'core:get_student_status' classid strdate %}" class="btn btn-sm btn-success"><i class="bi bi-eye me-1"></i>查看</a>
                </th>
            </tr>
        </tbody>
          <thead>
            <tr>
                <th>没交作业</th>
                {% for subject_name in subjects.subject_name_list%}
                <th>{{subject_name}}</th>
                {% endfor %}
                <th>详细</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><i class="bi bi-check-lg text-success"></td>
                {% for item in subjects.no_homework_count %}
                <td>{{item}}</td>
                {% endfor %}
                <td>
                      <a href="{% url 'core:class_homework_stats' classid strdate %}" class="btn btn-sm btn-success"><i class="bi bi-eye me-1"></i>查看</a>
                </td>
            </tr>
        </tbody>
    </table>
    
    <h6 class="mt-2">具体情况：</h6>
    <table class="table table-sm">
        <thead >
            <tr>
                <th>学生姓名</th>
                <th>迟到</th>
                <th>作业没交</th>
                <th>请假</th> 
                <th>违纪</th>
                <th>表彰</th>
                <th>详细</th>
            </tr>
        </thead>
        <tbody>
            {% for student in performance_stats %}
                <tr>
                    <td>{{ student.student_name }}</td>
                    <td>{% if student.late_count %}
                        <span class="text-danger">{{student.late_count}}次</span>
                        {% else %}
                        <span class="text-success">无</span>
                        {% endif %}
                    </td>
                    <td>{% if student.unsubmitted_homework %}
                        <span class="text-danger">{{student.unsubmitted_homework}}次</span>
                        {% else %}
                        <span class="text-success">无</span>
                        {% endif %}
                    </td>
                    <td>{% if student.leave_count %}
                        <span class="text-danger">{{student.leave_count}}次</span>
                        {% else %}
                        <span class="text-success">无</span>
                        {% endif %}
                    </td>
                    <td>{{student.discipline_count}}</td>
                    <td>{% if student.honor_count %}
                        <span class="text-danger">{{student.honor_count}}次</span>
                        {% else %}
                        <span class="text-success">无</span>
                        {% endif %}
                    </td>   
                    <td>
                        <a href="{% url 'core:student_performance' student_id=student.student_id  strdate=data %}" class="btn btn-sm btn-success"><i class="bi bi-eye me-1"></i>查看</a>

                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    
    <script>
        function fetchData() {
                $.ajax({
                    url: '{% url 'core:get_tasks' classid %}',
                    data: {
                        date: '{{strdate}}',
                        class_id: '{{classid}}',
                    },
                    method: 'GET',
                    dataType: 'json',
                    success: function(response) {
                         $('#task_list').empty();
                        $.each(response.data,function(index,item){
                            $('#task_number').text(`您有${response.data.length}个任务未处理`);
                           
                            $('#task_list').append(`<div class="alert alert-info" role="alert"></div>`);
                            $('#task_list div').append(`
                                <a href="/core/tasks/${item.id}/" class="alert-link">${item.content}</a>
                            `);
                           
                        });
                        // 请求成功后，设置下一次请求
                        setTimeout(fetchData, 5000);
                    },
                    error: function(error) {
                        console.error('请求出错:', error);
                        
                        // 请求失败时，也设置下一次请求，可以考虑增加重试间隔
                        setTimeout(fetchData, 10000); // 失败时延长到10秒后重试
                    }
                });
            }

            // 首次调用
            fetchData();
    </script>
{% endblock %}
